package com.leviassaf.androidbasics;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SqlLite extends Activity implements OnClickListener {
	
	Button btnUpdate, btnView, btnInsert;
	EditText edtId, edtName, edtHotness;
	TextView txtvwId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqllite);
		
		setJavaRefs();

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Bundle bndl = getIntent().getExtras();
		if (bndl != null){
			Person person = (Person)bndl.getParcelable("key");
			int intId = person.getId();
			String strName = person.getName();
			String strHotness = person.getHotness();
			
			txtvwId.setVisibility(View.VISIBLE);
			edtId.setVisibility(View.VISIBLE);
			btnUpdate.setVisibility(View.VISIBLE);
			btnInsert.setVisibility(View.GONE);
			
			edtId.setText(Integer.toString(intId));
			edtName.setText(strName);
			edtHotness.setText(strHotness);
			edtHotness.requestFocus();
			
			getIntent().removeExtra("key");
		}
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		btnUpdate = (Button)findViewById(R.id.btnUpdate);
		btnInsert = (Button)findViewById(R.id.btnInsert);
		btnView = (Button)findViewById(R.id.btnView);
		edtName = (EditText)findViewById(R.id.edtName);
		edtHotness = (EditText)findViewById(R.id.edtHotness);
		txtvwId = (TextView)findViewById(R.id.txtvwId);
		edtId = (EditText)findViewById(R.id.edtId);
		
		btnUpdate.setOnClickListener(this);
		btnInsert.setOnClickListener(this);
		btnView.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		boolean booItWorked;
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btnUpdate:
			//118
			booItWorked = true;
			try{
			//116
				Person editedPerson = new Person();
				editedPerson.setId(Integer.parseInt(edtId.getText().toString()));
				editedPerson.setName(edtName.getText().toString());
				editedPerson.setHotness(edtHotness.getText().toString());
				
				ScaleMeasure entry = new ScaleMeasure(this);
				entry.open();
				entry.update(editedPerson);
				entry.close();
			}
			catch(Exception e){
				booItWorked = false;
				//119
				showCustomDialog("Error updating database", e.toString());
			}
			finally{
				if (booItWorked){
					Toast.makeText(this, "Record Successfully Updated", Toast.LENGTH_SHORT).show();
				}
			}
			break;
		case R.id.btnInsert:
			//118
			booItWorked = true;
			try{
			//116
				Person NewPerson = new Person();
				NewPerson.setName(edtName.getText().toString());
				NewPerson.setHotness(edtHotness.getText().toString());
				
				ScaleMeasure entry = new ScaleMeasure(this);
				entry.open();
				entry.insert(NewPerson);
				entry.close();
			}
			catch(Exception e){
				booItWorked = false;
				//119
				showCustomDialog("Error updating database", e.toString());
			}
			finally{
				if (booItWorked){
					Toast.makeText(this, "Record Successfully Added", Toast.LENGTH_SHORT).show();
				}
			}
			break;
		case R.id.btnView:
			Intent intent = new Intent("com.leviassaf.speaktranslation.SQLVIEW");
			startActivity(intent);
			break;
		}
	}
	private void showCustomDialog(String Title, String Message){
		Dialog dialog = new Dialog(this);
		dialog.setTitle(Title);
		TextView txtvw = new TextView(this);
		txtvw.setText(Message);
		dialog.setContentView(txtvw);
		dialog.show();
	}
	
}
